package hu.dynamicwebshop.gui;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import hu.dynamicwebshop.contents.RegistrationContent;
import hu.dynamicwebshop.daos.impls.MenuDAOImpl;
import hu.dynamicwebshop.entities.Menu;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 * @author Szecskó Zoltán <zoltan.szecsko@gmail.com>
 */
public class ContentLayer extends VerticalLayout
        implements ApplicationContextAware {

    private static final long serialVersionUID = 1L;
    private String width = "690";
    private String height = "900";
    private ApplicationContext ctx;
    private MenuDAOImpl mdi;
    private MenuLayer ml;

    public ContentLayer() {
        setStyleName("contentLayer");
        setHeight(height);
        setWidth(width);
    }

    public void setAdminContent() {
        removeAllComponents();
        Button newMenuButton = new Button("Új menüpont felvitele");
        newMenuButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                menuCreator();
            }
        });
        addComponent(newMenuButton);

        Button menuSequenceButton = new Button("Menü sorrend beállítás");
        menuSequenceButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                menuSequenceChange();
            }
        });
        addComponent(menuSequenceButton);

        Button menuModifyButton = new Button("Menü szövegének módosítása");
        menuModifyButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                menuModify();
            }
        });
        addComponent(menuModifyButton);
    }

    public void setRegistrationContent() {
        RegistrationContent registrationContent = (RegistrationContent) ctx.getBean("registrationContent");
        registrationContent.createRegistrationLayer();
    }

    private void menuModify() {
        removeAllComponents();

        Label menuModifyLabel = new Label("Menü szövegének módosítása");
        addComponent(menuModifyLabel);

        MenuBar menu = ((MenuLayer) ctx.getBean("menuLayer")).getMenu();
        final List<MenuItem> menuItems = menu.getItems();
        final List<TextField> menuItemFields = new ArrayList<TextField>();

        for (MenuItem m : menuItems) {
            TextField textField = new TextField();
            textField.setValue(m.getText());
            menuItemFields.add(textField);
            addComponent(textField);
        }

        Button modifyButton = new Button("Módosítás");
        modifyButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                int i = 0;
                for (TextField tf : menuItemFields) {
                    if (!tf.getValue().equals(menuItems.get(i).getText())) {
                        Menu menu = new Menu();
                        menu.setMenuId(mdi.getMenuIdByName(menuItems.get(i).getText()));
                        menu.setMenuName(tf.getValue());
                        menu.setPlaceId(mdi.getPlaceIdByMenuName(menuItems.get(i).getText()));

                        mdi.modifyMenu(menu);
                    }
                    i++;
                }

                ml.refreshMenuBar();
            }
        });
        addComponent(modifyButton);
    }

    private void menuSequenceChange() {
        removeAllComponents();

        Label menuSequenceLabel = new Label("Menüpontok sorrendjének beállítása:");
        addComponent(menuSequenceLabel);

        MenuBar menu = ((MenuLayer) ctx.getBean("menuLayer")).getMenu();
        final List<MenuItem> menuItems = menu.getItems();
        final List<TextField> menuItemFields = new ArrayList<TextField>();

        //menüpontok neveinek kiíratása + mellé egy text field, amibe a sorrendet szabhatjuk meg
        for (MenuItem m : menuItems) {
            Label menuItemLabel = new Label(m.getText() + ": ");
            addComponent(menuItemLabel);
            TextField menuItemField = new TextField();
            menuItemFields.add(menuItemField);
            menuItemField.setValue(String.valueOf(mdi.getPlaceIdByMenuName(m.getText())));
            addComponent(menuItemField);
        }

        Button modifySequenceButton = new Button("Sorrend modósítása");
        modifySequenceButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                if (!examinePlaceIds(menuItemFields)) {
                    int i = 0;
                    for (MenuItem m : menuItems) {
                        Menu menu = new Menu();

                        menu.setMenuId(mdi.getMenuIdByName(m.getText()));
                        menu.setMenuName(m.getText());
                        menu.setPlaceId(Integer.valueOf(menuItemFields.get(i).getValue()));
                        mdi.modifyMenu(menu);
                        i++;
                    }
                    ml.refreshMenuBar();
                    menuSequenceChange();
                }
            }
        });

        addComponent(modifySequenceButton);
    }

    private void menuCreator() {
        removeAllComponents();

        Label menuName = new Label("Kérem a menüpont nevét: ");
        addComponent(menuName);

        final TextField menuNameField = new TextField();
        addComponent(menuNameField);

        Button menuCreateButton = new Button("Menüpont felvitele");
        menuCreateButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                MenuBar menu = ((MenuLayer) ctx.getBean("menuLayer")).getMenu();
                menu.addItem(menuNameField.getValue(), null);
                ((MenuLayer) ctx.getBean("menuLayer")).saveMenu();
                menuCreator();
            }
        });
        addComponent(menuCreateButton);
    }

    public boolean examinePlaceIds(List<TextField> menuItemFields) {
        boolean returnValue = false;
        for (TextField tf : menuItemFields) {
            if (searchForSameNumber(menuItemFields, tf.getValue(), false)) {
                ml.placeIdWarning("Ugyan olyan sorszámot adtál meg valamelyik menüponthoz!");
                returnValue = true;
                break;
            } else if (greaterThanMenuItemSize(menuItemFields, tf.getValue())) {
                ml.placeIdWarning("Nagyobb sorszámot adtál meg, mint az összes menüpontok száma!");
                returnValue = true;
                break;
            }
        }

        return returnValue;
    }

    public boolean greaterThanMenuItemSize(List<TextField> menuItemFields,
            String value) {
        boolean greater = false;

        for (TextField tf : menuItemFields) {
            if (Integer.valueOf(tf.getValue()) > menuItemFields.size()) {
                greater = true;
                break;
            }
        }

        return greater;
    }

    public boolean searchForSameNumber(List<TextField> menuItemFields,
            String value, boolean b) {
        int numberOfValue = 0;

        for (TextField tf : menuItemFields) {
            if (tf.getValue().equals(value)) {
                numberOfValue++;
            }
        }

        if (numberOfValue > 1) {
            b = true;
        }

        return b;
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        this.ctx = ctx;
        mdi = (MenuDAOImpl) ctx.getBean("menuDaoImpl");
        ml = (MenuLayer) ctx.getBean("menuLayer");
    }

    /**
     * Beállítja a felhasználóhoz tartozó adatokat az adatbázisból
     *
     * @param usernameField: felhasználó neve
     * @param passwordField: felhasználó jelszava
     */
    public void setUserContent(TextField usernameField, TextField passwordField) {

    }
}
